package cn.yao.hotel.config;


import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

/**
 * Copyright (C), 2022-2022, 姚兵
 * Author: 32210
 * Date: 2022/10/5 17:20
 * FileName: EsConfig
 * Description: 12
 */

@Configuration
public class EsConfig {

//    @Bean
//    public RestClient restClient(){
//        // Create the low-level client
//        RestClient httpClient = RestClient.builder(
//                new HttpHost("192.168.25.23", 9200)
//        ).build();
//        return httpClient;
//    }
//    @Bean
//    public RestHighLevelClient restHighLevelClient(){
//
//
//// Create the HLRC
//        RestHighLevelClient hlrc = new RestHighLevelClientBuilder(restClient())
//                .setApiCompatibilityMode(true)
//                .build();
//      return hlrc;
//    }


    @Bean
    public ElasticsearchClient elasticsearchClient() throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException {

//       Path caCertificatePath = Paths.get("E:\\http_ca.crt");
//       CertificateFactory factory =
//               CertificateFactory.getInstance("X.509");
//       Certificate trustedCa;
//       try (InputStream is = Files.newInputStream(caCertificatePath)) {
//           trustedCa = factory.generateCertificate(is);
//       }
//       KeyStore trustStore = KeyStore.getInstance("pkcs12");
//       trustStore.load(null, null);
//       trustStore.setCertificateEntry("ca", trustedCa);
//       SSLContextBuilder sslContextBuilder = SSLContexts.custom()
//               .loadTrustMaterial(trustStore, null);
//       final SSLContext sslContext = sslContextBuilder.build();
        final CredentialsProvider credentialsProvider =
                new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("elastic", "wIaMT+rW6Wn+kb2z*B8L"));
        // Create the low-level client
        RestClient restClient = RestClient.builder(
                        new HttpHost("192.168.25.23", 9200,"http"))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(
                            HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder
                                .setDefaultCredentialsProvider(credentialsProvider);
                    }
                }).build();


        // Create the Java API Client with the same low level client
        ElasticsearchTransport transport = new RestClientTransport(
                restClient,
                new JacksonJsonpMapper(

                        new ObjectMapper().setPropertyNamingStrategy(PropertyNamingStrategies.LOWER_CAMEL_CASE))
        );

        ElasticsearchClient esClient = new ElasticsearchClient(transport);

// hlrc and esClient share the same httpClient
        return esClient;
    }
}
